<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Common extends REST_Controller
{
    protected $rules = array(


    );

    private $rdata = array();

    function __construct(){
        session_start();
        parent::__construct();

        $this->load->model('common_model','common','default');
        $this->load->model('banner_model','banner');//banner表
        $this->load->model('user_model','user');//user表
        $this->load->model('upload_model','upload');
        $this->load->model('qualification_model','qualification');//资质表
        $this->load->model('equipment_model','equipment');//设备表

        $this->returnInfo = lang('returnInfo');
        $this->rdata['returnNo'] = '400';
        $this->rdata['returnInfo'] = '请求操作失败';
        $this->rdata['returnTime'] = time();
        $this->rdata['Secure'] = JSON_SECURE;
        $this->rdata['Content2'] = null;
        $this->rdata['Content'] = null;
        $this->rdata['returnData'] = null;
        $this->load->helpers('uisite_helper');
    }

    /**
     * 首页-企业banner
     * ShareId
     */
    public function banner_get(){
        $data = $this->get();//分享状态
        if(isset($this->uinfo)){
            $where = array('uid'=>$this->uinfo['id']);//登录状态
        }else{
            if(isset($data['ShareId'])){
                $share_user = $this->user->onceInfo(array('share_id'=>$data['ShareId']),'id');
                $where = array('uid'=>$share_user['id']);
            }else{
                // 无登录或分享状态 返回失败
                $this->rdata['ReturnCode'] = '400';
                $this->rdata['ReturnInfo'] = '无登录或分享状态';
                $this->response($this->rdata);
            }
        }
        $result_t = $this->db->select('id,title,link,photo,timeline')
            ->from("boc_banner")
            ->where($where)
            ->order_by('timeline', 'desc')
            ->get();
        $lists = $result_t->result_array();
        foreach($lists as $k=>$v){
            $photo_info = $this->upload->get_one(array('id'=>$v['photo']),'url');
            if($photo_info){
                $lists[$k]['thumb'] = UPLOAD_URL.$photo_info['url'];
            }else{
                $lists[$k]['thumb'] = '';
            }
        }
        $this->rdata['ReturnCode'] = '200';
        $this->rdata['ReturnInfo'] = '获取成功';
        $this->rdata['Content'] = $lists;
        $this->response($this->rdata);
    }

    /**
     * 首页-产品推荐
     * ShareId
     */
    public function flag_product_get(){

    }

    /**
     * 首页-资质信息前三条
     * ShareId
     */
    public function flag_qualification_get(){
        $data = $this->get();//分享状态
        if(isset($this->uinfo)){
            $where = array('uid'=>$this->uinfo['id']);//登录状态
        }else{
            if(isset($data['ShareId'])){
                $share_user = $this->user->onceInfo(array('share_id'=>$data['ShareId']),'id');
                $where = array('uid'=>$share_user['id']);
            }else{
                // 无登录或分享状态 返回失败
                $this->rdata['ReturnCode'] = '400';
                $this->rdata['ReturnInfo'] = '无登录或分享状态';
                $this->response($this->rdata);
            }
        }
        $result_t = $this->db->select('id,title,content,photo,timeline')
            ->from("boc_qualification")
            ->where($where)
            ->order_by('timeline','desc')
            ->limit(3)
            ->get();
        $lists = $result_t->result_array();
        foreach($lists as $k=>$v){
            $photo_info = $this->upload->get_one(array('id'=>$v['photo']),'url');
            if($photo_info){
                $lists[$k]['thumb'] = UPLOAD_URL.$photo_info['url'];
            }else{
                $lists[$k]['thumb'] = '';
            }
        }
        $this->rdata['ReturnCode'] = '200';
        $this->rdata['ReturnInfo'] = '获取成功';
        $this->rdata['Content'] = $lists;
        $this->response($this->rdata);
    }

    /**
     * 首页-设备清单前三条
     * ShareId
     */
    public function flag_equipment_get(){
        $data = $this->get();//分享状态
        if(isset($this->uinfo)){
            $where = array('uid'=>$this->uinfo['id']);//登录状态
        }else{
            if(isset($data['ShareId'])){
                $share_user = $this->user->onceInfo(array('share_id'=>$data['ShareId']),'id');
                $where = array('uid'=>$share_user['id']);
            }else{
                // 无登录或分享状态 返回失败
                $this->rdata['ReturnCode'] = '400';
                $this->rdata['ReturnInfo'] = '无登录或分享状态';
                $this->response($this->rdata);
            }
        }
        $result_t = $this->db->select('id,title,content,photo,timeline')
            ->from("boc_equipment")
            ->where($where)
            ->order_by('timeline','desc')
            ->limit(3)
            ->get();
        $lists = $result_t->result_array();
        foreach($lists as $k=>$v){
            $photo_info = $this->upload->get_one(array('id'=>$v['photo']),'url');
            if($photo_info){
                $lists[$k]['thumb'] = UPLOAD_URL.$photo_info['url'];
            }else{
                $lists[$k]['thumb'] = '';
            }
        }
        $this->rdata['ReturnCode'] = '200';
        $this->rdata['ReturnInfo'] = '获取成功';
        $this->rdata['Content'] = $lists;
        $this->response($this->rdata);
    }


    /**
     * 产品中心-列表
     *
     */
    public function product_list_get(){

    }

    /**
     * 资质信息-列表
     * ShareId Limit Page
     */
    public function qualification_list_get(){
        $data = $this->get();//分享状态
        if(isset($this->uinfo)){
            $where = array('uid'=>$this->uinfo['id']);//登录状态
        }else{
            if(isset($data['ShareId'])){
                $share_user = $this->user->onceInfo(array('share_id'=>$data['ShareId']),'id');
                $where = array('uid'=>$share_user['id']);
            }else{
                // 无登录或分享状态 返回失败
                $this->rdata['ReturnCode'] = '400';
                $this->rdata['ReturnInfo'] = '无登录或分享状态';
                $this->response($this->rdata);
            }
        }
        $page=isset($data['PageNo'])?$data['PageNo']:1;
        $limit=isset($data['Limit'])?$data['Limit']:10;
        $page = $limit*($page-1);
        $result_t = $this->db->select('id,title,content,photo,timeline')
            ->from("boc_qualification")
            ->where($where)
            ->order_by('timeline','desc')
            ->limit($limit,$page)
            ->get();
        $lists = $result_t->result_array();
        foreach ($lists as $k => $v) {
            $photo_info = UPLOAD_URL.tag_photo($v['photo']);
            if (UPLOAD_PATH.tag_photo($v['photo'])) {
                $lists[$k]['thumb'] = $photo_info;
            } else {
                $lists[$k]['thumb'] = '';
            }
        }
        $result['lists'] = $lists;
        $result['total'] = $this->qualification->countLog($where);

        $this->rdata['ReturnCode'] = '200';
        $this->rdata['ReturnInfo'] = '获取成功';
        $this->rdata['Content'] = $lists;
        $this->response($this->rdata);
    }

    /**
     * 设备清单-列表
     * ShareId Limit Page
     */
    public function equipment_list_get(){
        $data = $this->get();//分享状态
        if(isset($this->uinfo)){
            $where = array('uid'=>$this->uinfo['id']);//登录状态
        }else{
            if(isset($data['ShareId'])){
                $share_user = $this->user->onceInfo(array('share_id'=>$data['ShareId']),'id');
                $where = array('uid'=>$share_user['id']);
            }else{
                // 无登录或分享状态 返回失败
                $this->rdata['ReturnCode'] = '400';
                $this->rdata['ReturnInfo'] = '无登录或分享状态';
                $this->response($this->rdata);
            }
        }
        $page=isset($data['PageNo'])?$data['PageNo']:1;
        $limit=isset($data['Limit'])?$data['Limit']:10;
        $page = $limit*($page-1);
        //分享状态
        $result_t = $this->db->select('id,title,content,photo,timeline')
            ->from("boc_equipment")
            ->where($where)
            ->order_by('timeline','desc')
            ->limit($limit,$page)
            ->get();
        $lists = $result_t->result_array();
        foreach ($lists as $k => $v) {
            $photo_info = UPLOAD_URL.tag_photo($v['photo']);
            if (UPLOAD_PATH.tag_photo($v['photo'])) {
                $lists[$k]['thumb'] = $photo_info;
            } else {
                $lists[$k]['thumb'] = '';
            }
        }
        $result['lists'] = $lists;
        $result['total'] = $this->equipment->countLog($where);

        $this->rdata['ReturnCode'] = '200';
        $this->rdata['ReturnInfo'] = '获取成功';
        $this->rdata['Content'] = $lists;
        $this->response($this->rdata);
    }


    /**
     * 产品中心-详情
     *
     */
    public function product_info_get(){

    }

    /**
     * 资质信息-详情
     * ShareId Id
     */
    public function qualification_info_get(){
        $data = $this->get();//分享状态
        if(isset($this->uinfo)){
            $where = array('uid'=>$this->uinfo['id'],'id'=>$data['Id']);//登录状态
        }else{
            if(isset($data['ShareId'])){
                $share_user = $this->user->onceInfo(array('share_id'=>$data['ShareId']),'id');
                $where = array('uid'=>$share_user['id'],'id'=>$data['Id']);
            }else{
                // 无登录或分享状态 返回失败
                $this->rdata['ReturnCode'] = '400';
                $this->rdata['ReturnInfo'] = '无登录或分享状态';
                $this->response($this->rdata);
            }
        }

        $info = $this->qualification->onceInfo($where,'id,title,content,photo,timeline');
        if(empty($info)){
            // 无登录或分享状态 返回失败
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = '未查找到内容';
            $this->response($this->rdata);
        }
        $info['thumb']=duo_img($info['photo']);

        $this->rdata['ReturnCode'] = '200';
        $this->rdata['ReturnInfo'] = '获取成功';
        $this->rdata['Content'] = $info;
        $this->response($this->rdata);
    }

    /**
     * 设备清单-详情
     * ShareId Id
     */
    public function equipment_info_get(){
        $data = $this->get();//分享状态
        if(isset($this->uinfo)){
            $where = array('uid'=>$this->uinfo['id'],'id'=>$data['Id']);//登录状态
        }else{
            if(isset($data['ShareId'])){
                $share_user = $this->user->onceInfo(array('share_id'=>$data['ShareId']),'id');
                $where = array('uid'=>$share_user['id'],'id'=>$data['Id']);
            }else{
                // 无登录或分享状态 返回失败
                $this->rdata['ReturnCode'] = '400';
                $this->rdata['ReturnInfo'] = '无登录或分享状态';
                $this->response($this->rdata);
            }
        }

        $info = $this->equipment->onceInfo($where,'id,title,content,photo,timeline');
        if(empty($info)){
            // 无登录或分享状态 返回失败
            $this->rdata['ReturnCode'] = '400';
            $this->rdata['ReturnInfo'] = '未查找到内容';
            $this->response($this->rdata);
        }
        $info['thumb']=duo_img($info['photo']);

        $this->rdata['ReturnCode'] = '200';
        $this->rdata['ReturnInfo'] = '获取成功';
        $this->rdata['Content'] = $info;
        $this->response($this->rdata);
    }










}